package cn.wonderland.org.controller;

import cn.wonderland.basic.anno.PreAuthorize;
import cn.wonderland.basic.jwt.UserInfo;
import cn.wonderland.basic.util.AjaxResult;
import cn.wonderland.basic.util.LoginContext;
import cn.wonderland.basic.util.PageList;
import cn.wonderland.org.domain.Employee;
import cn.wonderland.org.query.EmployeeQuery;
import cn.wonderland.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /*查询所有*/
    @GetMapping
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    /*查询一个*/
    @GetMapping("/{id}")
    public Employee findById(@PathVariable Long id) {
        return employeeService.findById(id);
    }

    /*查询部门下的员工*/
    @GetMapping("/findByDeptId/{deptId}")
    public List<Employee> findByDeptId(@PathVariable Long deptId) {
        return employeeService.findByDeptId(deptId);
    }

    /**
     * 查询店铺下的员工
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> findListByShopId(@PathVariable("shopId") Long shopId) {
        return employeeService.findListByShopId(shopId);
    }

    /*根据id删除一个*/
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Long id) {
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    /*增加或者修改*/
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        if (employee.getId() == null) {
            // id为空执行新增
            employeeService.add(employee);
        } else {
            employeeService.update(employee);
        }
        return AjaxResult.me().success();
    }


    /*分页查询*/
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query) {
        UserInfo userInfo = LoginContext.getUserInfo();
        if (null != userInfo.getShop()) {
            query.setShopId(userInfo.getShop().getId());
        }
        return employeeService.queryByPage(query);
    }


    /*
     * 批量删除信息
     * */
    @PatchMapping()
    @PreAuthorize(name="员工批量删除",sn = "emp:batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        employeeService.batchDelete(ids);
        return AjaxResult.me().success();
    }
}
